package main

import (
	"fmt"
	"log"
	"net"
	"time"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"gitee.com/shexk/uninm/core"
	x "gitee.com/shexk/uninm/pluginmng"
)

func ClientEvent(obj any, conn net.Conn, state int, data []byte) {
	switch state {
	case core.Client_Connect:
		{
			core.GetLog().ShowLog("Client Connect,Addr=", conn.RemoteAddr().String())
		}
	case core.Client_Data:
		{
			core.GetLog().ShowLog("Client Data,Addr=", conn.RemoteAddr().String(),
				"Data=", data)
		}
	case core.Client_DisConnect:
		{
			core.GetLog().ShowLog("Client DisConnect,Addr=", conn.RemoteAddr().String())
			if p, ok := obj.(*core.TcpServer); ok {
				core.GetLog().ShowLog("Last Error=", p.GetLastError())
			}
		}
	case core.Client_Error:
		{
			if conn != nil {
				core.GetLog().ShowLog("Client Error,Addr=", conn.RemoteAddr().String())
			}

			if p, ok := obj.(*core.TcpServer); ok {
				core.GetLog().ShowLog("Last Error=", p.GetLastError())
			}

		}
	}
}
func main() {
	core.GetLog()
	server := core.NewTcpServer()
	server.OnEvent(server, ClientEvent).Run(60000)
	// client := core.NewTcpClient()
	// client.OnEvent(client, ClientEvent).Connect("127.0.0.1:60000")

	config := map[string]any{
		"plugin_dir": "./plugins",
		"log_level":  "info",
		"plugins": map[string]any{
			"hello-world-plugin": map[string]any{
				"greeting": "Hello from config",
			},
		},
	}

	// 创建并启动主程序
	h := x.NewHost(config)
	if err := h.Start(); err != nil {
		log.Fatalf("Failed to start host: %v", err)
	}
	defer func() {
		h.Close()
	}()

	// 创建一个新的Fyne应用
	myApp := app.New()
	// 创建主窗口，设置标题
	myWindow := myApp.NewWindow("Uninm")
	// 创建多行文本框
	textArea := widget.NewMultiLineEntry()

	scroll := container.NewScroll(textArea)
	scroll.SetMinSize(fyne.NewSize(800, 400)) // 设置最小尺寸

	numEdit := widget.NewEntry()
	sizeEdit := widget.NewEntry()
	clientNumEdit := widget.NewEntry()

	countContainer := container.NewGridWithColumns(8,
		widget.NewLabel("Plugins Num"),
		clientNumEdit,
		widget.NewLabel("Package Num"),
		numEdit,
		widget.NewLabel("Package Size"),
		sizeEdit,
	)

	go func() {
		timers := time.NewTicker(5 * time.Second)
		for range timers.C {
			fyne.Do(func() {
				// clientNumEdit.Text = fmt.Sprintf("%d", core.GetCount().ClientCount)
				// numEdit.Text = fmt.Sprintf("%d", core.GetCount().PackageCount)
				// sizeEdit.Text = core.GetValue(core.GetCount().PackageSize)
				clientNumEdit.Refresh()
				numEdit.Refresh()
				sizeEdit.Refresh()
			})
		}
	}()

	// 创建主容器，垂直排列按钮和文本框
	mainContainer := container.NewVBox(
		countContainer,
		scroll,
	)

	num := 0
	count := 0

	core.GetLog().OnEvent(func(log string) {
		fyne.Do(func() {
			num++
			count++
			s := fmt.Sprintf("[%d]", count)
			textArea.SetText(s + log + textArea.Text)
			if num >= 100 {
				textArea.SetText("")
				num = 0
			}
		})
	}).Run()

	myWindow.SetFixedSize(true)

	// 设置窗口内容并显示
	myWindow.CenterOnScreen()
	myWindow.SetContent(mainContainer)
	myWindow.Resize(fyne.NewSize(800, 400)) // 设置初始窗口大小

	myWindow.SetOnClosed(func() {

	})
	myWindow.Show()

	myApp.Run()

}

//打包程序：linux fyne package -os linux -icon app.ico
//Mac ../../../go/bin/fyne package -os darwin -icon app.ico
//windows fyne package -os windows -icon app.ico
